package org.liaohailong.library.http;

import androidx.annotation.NonNull;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import okhttp3.Interceptor;

/**
 * Author: liaohailong
 * Date: 2022/8/12
 * Time: 20:50
 * Description:
 **/
public class HttpConfig {
    int readTimeoutMs;
    int writeTimeoutMs;
    int connectTimeoutMs;
    boolean retryOnConnectionFailure;
    Map<String, String> headers = new HashMap<>();
    List<Interceptor> interceptors = new LinkedList<>();
    List<Interceptor> networkInterceptors = new LinkedList<>();

    public HttpConfig setConnectTimeoutMs(int connectTimeoutMs) {
        this.connectTimeoutMs = connectTimeoutMs;
        return this;
    }

    public HttpConfig setReadTimeoutMs(int readTimeoutMs) {
        this.readTimeoutMs = readTimeoutMs;
        return this;
    }

    public HttpConfig setWriteTimeoutMs(int writeTimeoutMs) {
        this.writeTimeoutMs = writeTimeoutMs;
        return this;
    }

    public HttpConfig setRetryOnConnectionFailure(boolean retryOnConnectionFailure) {
        this.retryOnConnectionFailure = retryOnConnectionFailure;
        return this;
    }

    public HttpConfig addHeader(String key, String value) {
        this.headers.put(key, value);
        return this;
    }

    public HttpConfig addInterceptor(@NonNull Interceptor interceptor) {
        interceptors.add(interceptor);
        return this;
    }

    public HttpConfig addNetworkInterceptor(@NonNull Interceptor interceptor) {
        networkInterceptors.add(interceptor);
        return this;
    }

    public static HttpConfig DEFAULT_TEXT = new HttpConfig()
            .setConnectTimeoutMs(1000 * 15)
            .setReadTimeoutMs(1000 * 15)
            .setWriteTimeoutMs(1000 * 15);

    public static HttpConfig DEFAULT_FILE = new HttpConfig()
            .setConnectTimeoutMs(1000 * 60)
            .setReadTimeoutMs(1000 * 90)
            .setWriteTimeoutMs(1000 * 90);
}
